<?php

class ArticleController extends AppController {

	public $name = 'article';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','show');

	}
	
	public function index() {
        $page = intval(Request::get_args('page'));	
        $cat_id = intval(Request::get_args('id'));
		$conditions = array();
        if($cat_id) {
		    $conditions['article.cat_id'] = $cat_id;
	    }		
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'article',
 		            //'joins' => array('type'=>'left','table'=>'shoprequest','conditions'=>array('shopinfo.user_id' =>'shoprequest.user_id')),
				    'fields' => array('article.*'),
					'conditions' => $conditions,
					'order' => 'article.id desc',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 	
		$query = array(
			'fields' => array('articlecat.*'), 
		);
		$catlist = POP::exe('[@@:UM articlecat][@@:QU find:all]',$query);
		$catinfo = array();
        foreach($catlist as $key => $value) {
            $catinfo[$value['articlecat']['id']] = $value['articlecat'];
        }    		
        $this->set('catinfo',$catinfo);	
        $this->set('result',$result);
        $this->set('catlist',$catlist);	
        $this->set('cat_id',$cat_id);			
	}	
	
	public function show($id) {
        $id = intval($id);
		
		$query = array(
			'conditions' => array('article.id' => $id), 
			'fields' => array('article.*'), 
		);
		$article_info = POP::exe('[@@:UM article][@@:QU find:first]',$query);			

	    $article_info = $article_info['article'];
		
		$article_updown = $this->article->find('neighbors');		

		$query = array(
			'fields' => array('articlecat.*'), 
		);
		$catlist = POP::exe('[@@:UM articlecat][@@:QU find:all]',$query);			
		
		
		$catinfo = array();
        foreach($catlist as $key => $value) {
            $catinfo[$value['articlecat']['id']] = $value['articlecat'];
        }    		
        $this->set('catinfo',$catinfo);	
        $this->set('article_info',$article_info);
        $this->set('article_updown',$article_updown);			
	}	
	
}